package com.jayqqaa12.jbase.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static final String HH_MM_SS = "HH:mm:ss";
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String MM_DD = "MM-dd";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String YY_MM_DD_HH_MM_SS = "yy-MM-dd HH:mm:ss";

	/**
	 * give a string date and plus 1 day
	 * @param data 
	 * @return
	 */
	public static String addDay(String data) {
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
		Calendar cd = Calendar.getInstance();
		try {
			cd.setTime(sdf.parse(data));
			cd.add(Calendar.DAY_OF_MONTH, 1);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sdf.format(cd.getTime());
	}
	
	/**
	 * add x days on today
	 * @param args
	 */
	public static String addDay(int add) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		cd.setTime(new Date());
		cd.add(Calendar.DAY_OF_MONTH, add);
		return sdf.format(cd.getTime());
	}

	/**
	 * Adds or subtracts the specified amount of time to the given date
	 * @param data
	 * @param add
	 * @return
	 */
	public static String addDay(String data, int add) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		try {
			cd.setTime(sdf.parse(data));
			cd.add(Calendar.DAY_OF_MONTH, add);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sdf.format(cd.getTime());
	}

	/**
	 * Adds or subtracts the specified amount of time to the given date
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date getAfterDate(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, amount);
		return cal.getTime();
	}

	public static int getMonthDay(int year, int month) {
		return getMonthDayByYear(year)[(--month)];
	}

	public static int[] getMonthDayByYear(int year) {
		int[] monthDay = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			monthDay[1] += 1;
		return monthDay;
	}

	/**
	 * @see #dateDiff(String,String,String)
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static long[] dateDiff(String startTime, String endTime) {
		return dateDiff(startTime, endTime, DateUtil.YYYY_MM_DD);
	}

	/**
	 * The difference between the two date
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @return array contains date info
	 */
	public static long[] dateDiff(String startTime, String endTime,
			String format) {
		SimpleDateFormat sd = new SimpleDateFormat(format);

		long nd = 86400000L;
		long nh = 3600000L;
		long nm = 60000L;
		long ns = 1000L;
		
		long[] date = new long[4];
		
		try {
			long diff = sd.parse(endTime).getTime()
					- sd.parse(startTime).getTime();

			long day = diff / nd;
			long hour = diff % nd / nh;
			long min = diff % nd % nh / nm;
			long sec = diff % nd % nh % nm / ns;

			date[0] = day;
			date[1] = hour;
			date[2] = min;
			date[3] = sec;
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * parse date
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date parseDate(String dateStr, String format) {
		Date date = null;
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			date = dateFormat.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String formatDate(String dateStr, String format,
			String toFormat) {
		return format(parseDate(dateStr, format), toFormat);
	}

	public static Date parseDate(String dateStr) {
		return parseDate(dateStr, "yyyy-MM-dd");
	}

	public static String format(Date date, String format) {
		String result = "";
		try {
			if (date != null) {
				DateFormat dateFormat = new SimpleDateFormat(format);
				result = dateFormat.format(date);
			}
		} catch (Exception localException) {
		}
		return result;
	}

	public static String format(Date date) {
		return format(date, DateUtil.YYYY_MM_DD);
	}

	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR);
	}

	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.SECOND);
	}

	public static long getMillis(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getTimeInMillis();
	}

	public static String getNow() {
		return getDate(new Date());
	}

	public static String getDate(Date date) {
		return format(date, DateUtil.YYYY_MM_DD);
	}

	public static String getTime(Date date) {
		return format(date, DateUtil.HH_MM_SS);
	}

	public static String getDateTime(Date date) {
		return format(date, DateUtil.YYYY_MM_DD_HH_MM_SS);
	}

	public static Date addDate(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		long millis = getMillis(date) + day * 24L * 3600L * 1000L;
		calendar.setTimeInMillis(millis);
		return calendar.getTime();
	}

	public static long diffDate(Date date, Date date1) {
		return (getMillis(date) - getMillis(date1)) / 86400000L;
	}

	public static String getMonthBegin(String strdate) {
		Date date = parseDate(strdate);
		return format(addDate(date, 0), "yyyy-MM") + "-01";
	}

	public static String getMonthEnd(String strdate) {
		Date date = parseDate(getMonthBegin(strdate));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return formatDate(calendar.getTime());
	}

	public static String formatDate(Date date) {
		return formatDateByFormat(date, DateUtil.YYYY_MM_DD);
	}

	public static String formatDateByFormat(Date date, String format) {
		String result = "";
		if (date != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(format);
				result = sdf.format(date);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	public static final int getCurrentDayOfWeek() {
		return Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
	}
}
